﻿using System;
using System.Web.UI;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using Evolution.Utilities;

namespace Evolution.UI
{
    public class ControlCollectionBuilder
    {
        #region Constructors

        public ControlCollectionBuilder(Control control)
        {
            if (control == null)
                throw new ArgumentNullException("control");

            this.Control = control;
            this.Text = new StringBuilder();
        }

        #endregion

        #region Properties

        public Control Control { get; private set; }

        public StringBuilder Text { get; private set; }

        public bool IsEmpty 
        {
            get { return this.Text.Length == 0; }
        }

        #endregion

        #region Methods

        public void AddStaticText(string text)
        {
            if (StringUtility.IsNullOrEmpty(text))
                return;

            this.Text.Append(text);
        }

        public void AddStaticText(string text, params object[] args)
        {
            if (StringUtility.IsNullOrEmpty(text))
                return;

            if (args == null)
                throw new ArgumentNullException("args");

            this.Text.AppendFormat(text, args);
        }
                
        public void AddControl(Control control)
        {
            if (control == null)
                throw new ArgumentNullException("control");

            if (!this.IsEmpty)
            {
                this.Control.Controls.Add(new LiteralControl(this.Text.ToString()));
                this.Clear();
            }

            this.Control.Controls.Add(control);
        }

        public void AddTextBoxAndBuilder(TextBox editor, bool showUrlPreview)
        {
            this.AddStaticText("<nobr>");

            this.AddControl(editor);

            this.AddStaticText(
                @"&nbsp;<input class=""ms-PropGridBuilderButton"" id=""INPUT-IDBuilder"" title=""Click to use builder"" style=""DISPLAY: inline"" onclick=""javascript:MSOPGrid_doBuilder('{0}', {1}, 'dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes');"" type=""button"" value=""..."" />",
                SPContext.Current.Web.Site.Url + "/_layouts/zoombldr.aspx?culture=en-US", editor.ClientID);

            this.AddStaticText("</nobr>");

            if (showUrlPreview)
                this.AddStaticText(
                    @"<BR /><DIV><A title=""Test Link (new window)"" onclick=""javascript:if(document.getElementById('{1}').value != '') window.open('{0}' + document.getElementById('{1}').value, 'TestLink');"" href=""#"">Test Link</A></div>",
                    SPContext.Current.Web.Site.Url + "/_layouts/Evolution/code/Preview.aspx?Source=", editor.ClientID);
        }

        public void AddErrorPlaceholder(string id)
        {
            if (StringUtility.IsNullOrEmpty(id))
                throw new ArgumentException("Error ID missing");

            LiteralControl error = new LiteralControl();
            error.ID = "Evolution_Pawp_" + id + "_Err";
            error.Text = "<div style=\"color: rgb(224, 0, 0);\"></div>";
            error.Visible = false;

            this.AddControl(error);
        }

        public void AddSeperator()
        {
            this.AddStaticText("<div class=\"UserDottedLine\" style=\"margin-top: 5px; margin-bottom: 5px;\"></div>");
        }

        protected void Clear()
        {
            this.Text.Length = 0;
        }

        public void Flush()
        {
            if (!this.IsEmpty)
            {
                this.Control.Controls.Add(new LiteralControl(this.Text.ToString()));
                this.Clear();
            }
        }

        #endregion
    }
}
